<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if(!class_exists('Api_volvo')){
    class Api_volvo extends CI_Controller {

        protected $_action = '';

        protected $_distance = 325;

        public function __construct(){
            parent::__construct();
            $this->load->model('Apilogmodel','m_api_log');
            $this->load->model('Usermodel','m_user');
            $this->load->model('Appmodel','m_app');
            $this->load->model('Volvocustomermodel','m_customer');
            $this->load->model('configmodel','m_config');
            $this->load->database();

            if(is_string($this->uri->rsegments[2]) && stripos($this->uri->rsegments[2], 'api') === 0) {
                return;
            }
            header('Access-Control-Allow-Origin: *');
        }

        public function __destruct() {
            // $this->session->unset_userdata($this->_action);
            // $customer = $this->session->userdata('volvo_customer');
            $info = $this->input->post();
            $result = $this->response->getResult();
            @$this->m_api_log->createApiLog(array(
                'ip_address' => $this->input->ip_address(),
                // 'customer_id' => empty($customer) ? 0 :  $customer['customer_id'],
                'request_uri' => $_SERVER['REQUEST_URI'],
                'post_data' => json_encode($info,JSON_UNESCAPED_UNICODE),
                'result_data' => json_encode($result,JSON_UNESCAPED_UNICODE),
                'api_log_time_create' => NOW,
                'ua' => empty($_SERVER['HTTP_USER_AGENT']) ? '' : $_SERVER['HTTP_USER_AGENT'],
                // 'terminal' => TERMINAL_WEIXIN,
            ));

            $this->db->close();
        }


        public function initCustomer() {
            $this->validate->setRule('customer_name','string');
            $this->validate->setRule('customer_phone','string');
            $this->validate->setRule('customer_sex','integer');
            $this->validate->setRule('customer_province','string');
            $this->validate->setRule('customer_city','string');
            $this->validate->setRule('customer_shop','string');
            if(!$this->validate->run($this->input->post(),true)){
                $this->response->setDetail($this->validate->getMessage());
                $this->response->responseExit();
            }
            $info = $this->validate->getResult();

            if(!preg_match("/^1[0-9]\d{9}$/i", $info['customer_phone'])){
                $this->response->setDetail('请输入正确的手机号码!');
                $this->response->responseExit();
            }

            $info['customer_time_create'] = NOW;
            $this->m_customer->createCustomer($info);
            

            $this->response->setSuccess();
            $this->response->setDetail('提交成功!');
            $this->response->setAddon(array(
            ));
            $this->response->responseExit();
        }

        
    }
    
}